package common;


import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.Cookie;

import com.google.gson.Gson;

import domainLogic.Item;

public class JspUtil {
	
	public final static String URL_DEFAULT = "/timsauction/index.jsp";
	public final static String URL_CONTENT_PAGE = "/timsauction";
	//public final static String URL_IMAGE = "e:/programming/project/auction/WebContent/pics/";
	public final static String URL_IMAGE = "C:/apache-tomcat-7.0.29/webapps/timsauction/";	
	
	public static boolean isLoggedIn(javax.servlet.http.HttpSession session)
	{
		String loginCheck_username = (String)session.getAttribute("username");

		if(loginCheck_username == null)
		{
			return false;
		}
		return true;
	}

	public static String getLoggedInUserId(javax.servlet.http.HttpSession session)
	{
		return getLoggedInUserName(session);
	}
	public static String getLoggedInUserName(javax.servlet.http.HttpSession session)
	{
		String username = (String)session.getAttribute("username");
		return username;
	}
	public static String makeErrorMsg(String msg, String url)
	{
		String ret = "<script>	alert(\""+ msg + "\");"
				+ "window.location = \"" + url + "\";"
				+ "</script>";
		return ret;
	}
	public static String makeErrorMsg(String msg)
	{
		String ret = "<script>	alert(\""+ msg + "\");"
				+ "history.back();"
				+ "</script>";
		return ret;
	}
	
	
	public static Cookie findCookie(javax.servlet.http.HttpServletRequest request, String cookiename)
	{
		Cookie [] cookies = request.getCookies();
		Cookie ret = null;
		if(cookies != null) // Find a cookie
		{
			for(Cookie c : cookies)	{
				if(c.getName().equals(cookiename))	{
					ret = c;
					break;
				}
			}
		}		
		return ret;
	}
	
	public static class RecentItem
	{
		public int item_id;		public String item_name;
		public RecentItem(int id, String name) 
		{ 
			this.item_id = id; 
			this.item_name = name;	
		}		
	}
	private static List<RecentItem> convertJsonStringToList (String jsonString)
	{
		Gson gson = new Gson();
		Type collectionType = new com.google.gson.reflect.TypeToken<ArrayList<RecentItem>>(){}.getType();
		ArrayList<RecentItem> recentItems = gson.fromJson(jsonString, collectionType);
		return recentItems;	
	}
	private static String convertListToJsonString (List<RecentItem> list)
	{
		Gson gson = new Gson();
		return gson.toJson(list);
	}
	
	private final static String RECENT_ITEMS_COOKIE_NAME = "recently_view_json2";
	private final static int RECENT_ITEMS_NUM = 3;
	
	public static List<RecentItem> getRecentItems(javax.servlet.http.HttpServletRequest request)
	{
		Cookie riCookie = findCookie(request, RECENT_ITEMS_COOKIE_NAME);
		
		if(riCookie == null)
			return null;
		
		String riJsonString = riCookie.getValue();
		List<RecentItem> ret = null;
		
		try{
			ret = convertJsonStringToList(riJsonString);
		}catch (Exception e)
		{
			e.printStackTrace();			
		}
		return ret;
	}
	public static void addRecentItems(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, Item addItem)
	{
		List<RecentItem> recentItems = getRecentItems(request);
		
		if(recentItems == null)
			recentItems = new ArrayList<RecentItem>();
		
		// Check duplicate, if there is, remove it and add new one at the first (change order)
		for(int i = 0; i<recentItems.size(); i++)
		{
			if(recentItems.get(i).item_id == addItem.getId())
			{
				recentItems.remove(i);
			}
		}
		RecentItem newitem =  new RecentItem(addItem.getId(), addItem.getName().replaceAll("\"", "\\\""));
		
		recentItems.add(0, newitem);
		
		// Remain the number of items.
		if(recentItems.size() > RECENT_ITEMS_NUM)
			recentItems.remove(RECENT_ITEMS_NUM);
		
		String riJsonString = convertListToJsonString(recentItems).replaceAll("\\\"","\\\\\"");
		
		Cookie cookie = new Cookie(RECENT_ITEMS_COOKIE_NAME, riJsonString);
		cookie.setMaxAge(24*60*60);	//valid for one day
		response.addCookie(cookie);
	}
		
}
